package edu.cmu.vlis.wassup.db;

/**
 * @author Radhika
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnector {

	private Connection dbConnection = null;;
	private String database;
	private String server;
	private String driver;
		
	
	/**
	 * 
	 * @param dbName
	 * @param serverip
	 */
	public DBConnector( String dbName, String serverip ) 
	{
		dbConnection = null;
		
		driver = "com.mysql.jdbc.Driver";
		database = dbName;
		server = serverip;
		
		setConnection();
	}
	
	/**
	 * Utility Function
	 *
	 */
	private void setConnection ()
	{
		try 
		{
			Class.forName( driver );
			
	        String url = "jdbc:mysql://" + server + "/" + database + "?user=root&password=wassup";
	        
	        dbConnection = DriverManager.getConnection( url );
	        
		} 
		catch ( ClassNotFoundException clnFound ) {
			System.out.println( " Driver class not found " ) ;
		} 
		catch ( SQLException sqle ) {
			System.out.println( " Connecttion can not be made" ) ;
		}
	}
	
	/**
	 * 
	 * @return Connection
	 */
	final public Connection getConnection()
	{
		setConnection();
		return dbConnection;
		
	}
	
	public void close()
	{
		try
		{		
			dbConnection.close();
		}
		catch( SQLException sqle )
		{
			System.out.println( "Error in closing connection " + sqle ) ;
		}
	}
	
}

